<?php
/**
 * 菜单视图
 */


use backend\services\MenuService;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;

//用户信息
$controller = Yii::$app->controller;
$userInfo = ArrayHelper::getValue(Yii::$app->params, 'userInfo');
if(empty($userInfo)) {
    $controller->redirect(['/login/index'])->send();
}
// 获取菜单列表
if(!isset(Yii::$app->params['menu'])) {
    $menuService = new MenuService();
    $menuService->setMenu($userInfo['id']);

    $menu = isset(Yii::$app->params['menu']) ? Yii::$app->params['menu'] : [];
    $activeMenuId = isset(Yii::$app->params['activeMenuId']) ? Yii::$app->params['activeMenuId'] : [];
}
?>
<div class="navbar-default sidebar" role="navigation">
    <div class="sidebar-nav navbar-collapse">
        <ul class="nav" id="side-menu">
            <li class="sidebar-search">
                <div class="input-group custom-search-form">
                    <input type="text" class="form-control" placeholder="Search...">
                    <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <i class="fa fa-search"></i>
                                </button>
                            </span>
                </div>
                <!-- /input-group -->
            </li>
            <li>
                <a href="<?= Url::toRoute(['/index/index'])?>"><i class="fa fa-home fa-fw"></i> 首页</a>
            </li>
            <?php foreach($menu as $key => $item):?>
            <li <?php echo is_array($activeMenuId) && in_array($item['id'], $activeMenuId) ? 'class="active"': '';?>>
                <a href="<?=($url = ArrayHelper::getValue($item, 'url')) ? Url::to([$url]) : 'javascript:void(0);'?>">
                    <i class="fa <?= ArrayHelper::getValue($item['options'], 'icon', 'fa-list-ul')?> fa-fw"></i>
                    <?php echo ArrayHelper::getValue($item, 'label')?>
                    <?php if ($item['items']):?><span class="fa arrow"></span><?php endif;?>
                </a>
                <?php if ($item['items']):?>
                    <ul class="nav nav-second-level collapse">
                        <?php foreach($item['items'] as $k => $childItem):?>
                            <li <?= is_array($activeMenuId) && in_array($childItem['id'], $activeMenuId) ? 'class="active"': '';?>>
                                <a href="<?= ($url = ArrayHelper::getValue($childItem, 'url')) ? Url::to([$url]) : 'javascript:void(0);'?>">
                                    <?= ArrayHelper::getValue($childItem, 'label')?>
                                    <?php if ($childItem['items']):?>
                                    <span class="fa arrow"></span>
                                    <?php endif;?>
                                </a>
                                <?php if ($childItem['items']):?>
                                <ul class="nav nav-third-level">
                                    <?php foreach($childItem['items'] as $thirdItem):?>
                                    <li>
                                        <a href="<?= ($url = ArrayHelper::getValue($thirdItem, 'url')) ? Url::to([$url]) : 'javascript:void(0);'?>"><?= ArrayHelper::getValue($thirdItem, 'label')?></a>
                                    </li>
                                    <?php endforeach;?>
                                </ul>
                                <?php endif;?>
                            </li>
                        <?php endforeach;?>
                    </ul>
                <?php endif;?>
            </li>
            <?php endforeach;?>
        </ul>
    </div>
    <!-- /.sidebar-collapse -->
</div>
<!-- /.navbar-static-side -->